package com.iasku.fragment;

import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.iasku.adapter.HomeWorkAdapter;
import com.iasku.constant.Constant;
import com.iasku.constant.IntentConfig;
import com.iasku.entity.UserHomework;
import com.iasku.iaskueleclass_phone.R;
import com.iasku.iaskueleclass_phone.ReturnData;
import com.iasku.iaskueleclass_phone.Status;
import com.iasku.iaskueleclass_phone.activity.HomeworkCorrectedActivity;
import com.iasku.iaskueleclass_phone.activity.HomeworkDoingActivity;
import com.iasku.iaskueleclass_phone.activity.HomeworkSubmitActivity;
import com.iasku.manager.DataManager;
import com.iasku.manager.IDataManager;
import com.iasku.manager.IaskuManager;

public class HomeworkFragment extends BaseFragment{
    private RadioGroup mRadioGroup;
    private RadioButton mNoDoingRb,mIsSubmitRb,mIsCorrectRb;
    private List<UserHomework> mWorkList;
    private ListView mHomeworkLv;
    private TextView title1,title2,title3,title4;
    
    private SharedPreferences mPref;
    
    //定义按钮字体颜色
    private static final int TEXT_COLOR_NODOING = 1;
    private static final int TEXT_COLOR_ISSUBMIT = 2;
    private static final int TEXT_COLOR_ISCORRECT = 3;
    
    //定义获取作业的状态
    private static final int STATUS_WORK_NODOING = 0;
    private static final int STATUS_WORK_ISSUBMIT = 1;
    private static final int STATUS_WORL_ISCORRECT = 2;
    
    private static final int TASK_GET_HOMEWORK = 1;
    
    private int mStatusWork;
    
    private HomeWorkAdapter mAdapter;
    
    private void setupView(View view){
        mRadioGroup = (RadioGroup) view.findViewById(R.id.homework_check_radiogroup);
        mHomeworkLv = (ListView) view.findViewById(R.id.homework_listview);
        mNoDoingRb = (RadioButton) view.findViewById(R.id.btn_homework_nodoing);
        mIsSubmitRb = (RadioButton) view.findViewById(R.id.btn_homework_issubmit);
        mIsCorrectRb = (RadioButton) view.findViewById(R.id.btn_homework_iscorrect);
        title1 = (TextView) view.findViewById(R.id.tv_worktitle1);
        title2 = (TextView) view.findViewById(R.id.tv_worktitle2);
        title3 = (TextView) view.findViewById(R.id.tv_worktitle3);
        title4 = (TextView) view.findViewById(R.id.tv_worktitle4);
    }
    
    private void setTitleValue(){
        title1.setText("教师姓名");
        title2.setText("作业名称");
        title3.setText("布置时间");
        title4.setText("完成时间");
        switch (mStatusWork) {
        case STATUS_WORK_NODOING:
            title4.setVisibility(View.GONE);
            break;

        case STATUS_WORK_ISSUBMIT:
            title4.setVisibility(View.VISIBLE);
            break;
        case STATUS_WORL_ISCORRECT:
            title4.setVisibility(View.VISIBLE);
            title3.setText("完成时间");
            title4.setText("得分");
            break;
        }
    }
    
    private void addListener(){
        mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                case R.id.btn_homework_nodoing:
                    setTextColor(TEXT_COLOR_NODOING);
                    mStatusWork = STATUS_WORK_NODOING;
                    break;
                case R.id.btn_homework_issubmit:
                    setTextColor(TEXT_COLOR_ISSUBMIT);
                    mStatusWork = STATUS_WORK_ISSUBMIT;
                    break;
                case R.id.btn_homework_iscorrect:
                    setTextColor(TEXT_COLOR_ISCORRECT);
                    mStatusWork = STATUS_WORL_ISCORRECT;
                    break;
                }
                setTitleValue();
                startTask(TASK_GET_HOMEWORK);
            }
        });
        
        mBarSearchBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startTask(TASK_GET_HOMEWORK);
            }
        });
        
        mHomeworkLv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UserHomework work = mWorkList.get(position);
                String examid = work.getExamid();
                String examname = work.getExamname();
                Intent intent = new Intent();
                intent.putExtra(IntentConfig.EXAMID, examid);
                intent.putExtra(IntentConfig.EXAMNAME, examname);
                switch (mStatusWork) {
                case STATUS_WORK_NODOING:
                    intent.setClass(getActivity().getApplicationContext(), HomeworkDoingActivity.class);
                    break;
                case STATUS_WORK_ISSUBMIT:
                    intent.setClass(getActivity().getApplicationContext(), HomeworkSubmitActivity.class);
                    break;
                case STATUS_WORL_ISCORRECT:
                    intent.setClass(getActivity().getApplicationContext(),HomeworkCorrectedActivity.class);
                    break;
                }
                startActivity(intent);
               
            }
        });
    }
    
    @Override
    public void onResume() {
        super.onResume();
        if(Constant.EXAM_IS_SUBMIT){
            if(mAdapter != null && mStatusWork == STATUS_WORK_NODOING){
                mAdapter.notifyDataSetChanged();
            }
        }
    }
    
    private void setTextColor(int color){
        int color_normal = getResources().getColor(R.color.fragment_homework_button_text);
        int color_pressed = Color.WHITE;
        mNoDoingRb.setTextColor(color_normal);
        mIsSubmitRb.setTextColor(color_normal); 
        mIsCorrectRb.setTextColor(color_normal);
        switch (color) {
        case TEXT_COLOR_NODOING:
            mNoDoingRb.setTextColor(color_pressed);
            break;
        case TEXT_COLOR_ISSUBMIT:
            mIsSubmitRb.setTextColor(color_pressed);
            break;
        case TEXT_COLOR_ISCORRECT:
            mIsCorrectRb.setTextColor(color_pressed);
            break;
        }
    }
    
    private void initView(View view){
        mPref = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
        setupView(view);
        addListener();
        mStatusWork = STATUS_WORK_NODOING;
        setTitleValue();
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.homework_fragment_layout, null);
        initTitleBar(R.string.homework_title);
        initView(mRootView);
        startTask(TASK_GET_HOMEWORK);
        return mRootView;
    }
    
    @Override
    public int onTaskComplete(int taskId, Status status) {
        switch (taskId) {
        case TASK_GET_HOMEWORK:{
            mProgressDialog.dismiss();
            if(mWorkList != null){
                mHomeworkLv.setVisibility(View.VISIBLE);
                mAdapter = new HomeWorkAdapter(getActivity().getApplicationContext(),mWorkList);
                mHomeworkLv.setAdapter(mAdapter);
            }
        }
        break;
        }
        // TODO Auto-generated method stub
        return super.onTaskComplete(taskId, status);
    }
    
    @Override
    public int onTaskStart(int taskId, Bundle bundle) {
        if(taskId == TASK_GET_HOMEWORK){
            mHomeworkLv.setVisibility(View.INVISIBLE);
            mProgressDialog.show();
        }
        return super.onTaskStart(taskId, bundle);
    }
    
    @Override
    public Status onTaskInBackground(int taskId, Bundle bundle) {
        // TODO Auto-generated method stub
        switch (taskId) {
        case TASK_GET_HOMEWORK:{
            IDataManager manager = DataManager.getInstance();
            initTime();
            ReturnData<List<UserHomework>> mReturnData = manager.getMyHomework(Constant.USER_IASKUNAME,
                    IaskuManager.getInstance().getSubjectId(), mStatusWork,
                            String.valueOf(time_start),String.valueOf(time_end), mPref);
            mWorkList = mReturnData.getData();
            return new Status(mReturnData);
        }
        }
        return super.onTaskInBackground(taskId, bundle);
    }
}
